home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / PageTool 1.0 / Page.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  7KB  |  265 lines

  1. /*
  2.     File:        Page.c
  3.  
  4.     Contains:    main page tool contents
  5.  
  6.     Written by:    Brian Topping
  7.  
  8.     Copyright:    © 1993 by Brian Topping, all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                  6/17/93    BET        added optional zone string plus server instead of
  13.                                      just zone with assumed server name.  This hasn't
  14.                                     been tested because I don't have PagerPro at MacHack,
  15.                                     but it isn't rocket science either!
  16.  
  17.     To Do:
  18. */
  19.  
  20. #include    <Types.h>
  21. #include     <ctype.h>
  22. #include     <string.h>
  23. #include     <Strings.h>
  24. #include     <stdio.h>
  25. #include    <CursorCtl.h>
  26. #include    <Errors.h>
  27. #include    <AppleEvents.h>
  28. #include    <GestaltEqu.h>
  29. #include    <Script.h>
  30. #include    <PLStringFuncs.h>
  31. #include    <PPCToolBox.h>
  32. #include    <Fonts.h>
  33. #include    "PagerPROAECalls.h"
  34. #include    "AutoGuest.h"
  35.  
  36.  
  37. AEAddressDesc        targetAddress;                    /* target address */
  38. long                userRefNum;                        /* user ref num */
  39. char                *toolName;
  40.  
  41.  
  42. showError(OSErr theError)
  43. {
  44.     printf("### %s - an error occurred: %d\n",toolName,theError);
  45. }
  46.  
  47. initPPSData ()
  48. {    
  49.     /* AppleEvent data */
  50.     targetAddress.dataHandle = nil;
  51.     userRefNum = 0;
  52. }
  53.  
  54. /*----------------------------------------------------------------------
  55.     
  56.     This function shows the recommended way that your application
  57.     should use the authentication patches
  58.     
  59.     Since these routines patch traps which change the behavior of
  60.     the OS, it is a good idea to avoid installing them whenever
  61.     possible.  Ideally, your application will only call the
  62.     function 'InitialAESend' when it knows that it must start a
  63.     new session (i.e., it has a new address that it has never
  64.     messaged to before); at other times, it should call AESend
  65.     directly.
  66.     
  67. ----------------------------------------------------------------------*/
  68. OSErr InitialAESend( AppleEvent* question, AppleEvent* reply, AESendMode sendMode, AESendPriority sendPriority, long timeOutTicks, IdleProcPtr idleProc, EventFilterProcPtr filterProc )
  69. {
  70.     long        oldPPC;
  71.     long        oldNoInteraction;
  72.     Boolean        useNoInteractionPatch;
  73.     OSErr        theErr;
  74.     
  75.     /*
  76.     // Install the two AutoGuest patches.  Only put in
  77.     // the 'noInteractionPatch' if we really need it
  78.     */
  79.     useNoInteractionPatch = !InForeGround();
  80.     oldPPC = InstallPPCAutoGuest();
  81.     if( useNoInteractionPatch )
  82.         oldNoInteraction = InstallNoInteractionPatch();
  83.     
  84.     /*
  85.     // Now that we have installed the patch or patches,
  86.     // try the AESend
  87.     */
  88.     theErr = AESend( question, reply, sendMode, sendPriority, timeOutTicks, idleProc, filterProc );
  89.  
  90.     /*
  91.     // Deinstall any patch that we installed
  92.     */
  93.     if( useNoInteractionPatch )
  94.         DeinstallNoInteractionPatch(oldNoInteraction);
  95.     DeinstallPPCAutoGuest(oldPPC);
  96.     
  97.     return theErr;
  98. }
  99.  
  100.  
  101. OSErr    doesAddressExistInitial ( addString, retValue, targetAddress, errString)
  102. StringPtr        addString;            /* User or group name */
  103. long            *retValue;            /* Returned value */
  104. AEAddressDesc    *targetAddress;        /* Target application address */
  105. StringPtr        errString;            /* Returned error string */
  106. {
  107.     AppleEvent        theAppleEvent;
  108.     AppleEvent        reply;
  109.     AEDescList        param1List;
  110.     AEDesc            param1;
  111.     Size            actualSize;
  112.     DescType        returnedType;
  113.     OSErr            retError;
  114.     
  115.     errString [0] = 0;
  116.     reply.dataHandle = nil;
  117.     
  118.     /* create a CoreEvent, DoObjectsExist */
  119.     retError = AECreateAppleEvent ( kCoreEventClass, kAEDoObjectsExist, targetAddress, kAutoGenerateReturnID, kAnyTransactionID, &theAppleEvent);
  120.     if ( retError == noErr )
  121.     {
  122.         /* param1: address name */
  123.         retError = AECreateList ( nil, 0, false, ¶m1List);
  124.         retError = AECreateDesc ( typeChar, (Ptr) &addString [1], addString [0], ¶m1);
  125.         retError = AEPutDesc ( ¶m1List, 0, ¶m1);
  126.         retError = AEPutParamDesc ( &theAppleEvent, keyDirectObject, ¶m1List);
  127.         
  128.         /* send the event */
  129.         retError = AESend ( &theAppleEvent, &reply, kAEWaitReply + kAENeverInteract, kAENormalPriority, 120, nil, nil);
  130.         if ( retError == noErr )
  131.         {
  132.             /* get the size */
  133.             if ( AEGetParamPtr ( &reply, keyAEResult, typeLongInteger, &returnedType, (Ptr) retValue, sizeof(long), &actualSize) != noErr )
  134.             {
  135.                 /* get the error message */
  136.                 AEGetParamPtr ( &reply, keyErrorString, typeChar, &returnedType, (Ptr) errString, 255, &actualSize);
  137.                 
  138.                 errString [actualSize] = nil;
  139.                 c2pstr ( (Ptr) errString);
  140.                 
  141.                 retError = errAEEventFailed;
  142.             }
  143.         }
  144.         
  145.         if ( param1.dataHandle )
  146.             AEDisposeDesc ( ¶m1);
  147.         if ( reply.dataHandle )
  148.             AEDisposeDesc ( &reply);
  149.     }
  150.     
  151.     return ( retError);
  152. }
  153.  
  154. OSErr    initPagerPROAEInterfaceNoDialog (AEAddressDesc *targetAddress, LocationNameRec *theLocation, PortInfoRec *thePortInfo)
  155. {
  156.     OSErr                retError;
  157.     unsigned long        defUserRefNum;
  158.     Str32                userName;
  159.     IPCListPortsPBRec    ipcPB;
  160.     PortInfoRec            localPortInfo;
  161.     TargetID            targetID;
  162.  
  163.     /* select a PagerPRO to link to */
  164.     ipcPB.startIndex = 0;
  165.     ipcPB.requestCount = 1;
  166.     ipcPB.portName = &(thePortInfo->name);
  167.     ipcPB.locationName = theLocation;
  168.     ipcPB.bufferPtr = &localPortInfo;
  169.     if ((retError = IPCListPorts(&ipcPB,false)) == noErr)
  170.     {
  171.         if  (ipcPB.actualCount != 1)
  172.             return(69);
  173.             
  174.         /* prepare target address */
  175.         BlockMove ( &localPortInfo.name, &targetID.name, sizeof(PPCPortRec));
  176.         BlockMove ( theLocation, &targetID.location, sizeof(LocationNameRec));
  177.         retError = AECreateDesc ( typeTargetID, (Ptr) &targetID, sizeof(TargetID), targetAddress);
  178.         userName [0] = 0;
  179.         defUserRefNum = 0;
  180.     }
  181.     return ( retError);    
  182. }
  183.  
  184. getPPCData (char *zoneStr, char *serverStr)
  185. {
  186.     OSErr                retError;
  187.     LocationNameRec        theLocation;
  188.     PortInfoRec            thePortInfo;
  189.     long                aeAttribute;
  190.     
  191.     /* see if ppc need initialization */
  192.     retError = Gestalt ( gestaltAppleEventsAttr, &aeAttribute);
  193.     if ( retError == noErr )
  194.     {
  195.         if ( (aeAttribute & gestaltAppleEventsPresent) == 0 )
  196.         {
  197.             /* select a PagerPRO to link to */
  198.             thePortInfo.name.nameScript = smRoman;
  199.             PLstrcpy(thePortInfo.name.name, "\pPagerPRO Server");
  200.             
  201.             thePortInfo.name.portKindSelector = ppcByString;
  202.             PLstrcpy(thePortInfo.name.u.portTypeStr,"\p=");
  203.             
  204.             theLocation.locationKindSelector = ppcNBPLocation;
  205.             PLstrcpy(theLocation.u.nbpEntity.objStr,serverStr);
  206.             PLstrcpy(theLocation.u.nbpEntity.typeStr, "\pPPCToolBox");
  207.             PLstrcpy(theLocation.u.nbpEntity.zoneStr, zoneStr);
  208.         
  209.             retError = initPagerPROAEInterfaceNoDialog (&targetAddress,&theLocation,&thePortInfo);
  210.         }
  211.     }
  212.     
  213.     return (retError);    
  214. }
  215.  
  216. main(int argc, char *argv[])
  217. {
  218. char            user[64],message[64],zone[64],server[64],errString[256];
  219. long             retValue;
  220. OSErr            theError;
  221.  
  222.     toolName = argv[0];
  223.     printf("PageTool by Brian Topping\n",toolName);
  224.     if (argc != 4) {
  225.         printf("### %s -- Error in parameters\n",toolName);
  226.         printf("### %s -- Usage \"%s <[serverZone:]serverName> <user> <message>\"\n",toolName,toolName);
  227.         return 1;
  228.         }
  229.     
  230.     if (sscanf("%s:%s",zone,server) != 2) 
  231.         strncpy(zone,argv[1],64);
  232.     else
  233.         c2pstr(server);
  234.         
  235.     c2pstr(zone);
  236.     
  237.     strncpy(user,argv[2],64);
  238.     c2pstr(user);
  239.     
  240.     strncpy(message,argv[3],64);
  241.     c2pstr(message);
  242.     
  243.     InitGraf ( &qd.thePort);
  244.     SetFScaleDisable(true);
  245.     
  246.     initPPSData ();
  247.     if ( (theError = getPPCData(zone,server)) != noErr ) {
  248.         showError(theError);
  249.         return 2;
  250.         }
  251.     
  252.     doesAddressExistInitial ( (StringPtr) "\pany address", &retValue, &targetAddress, errString);
  253.     
  254.     if (theError = doAddressExist ( user, &retValue, &targetAddress, errString)){
  255.         showError(theError);
  256.         return 2;
  257.         }
  258.         
  259.     if (theError = sendMessage ( NORMALPRIORITY, user, message, &targetAddress, errString)){
  260.         showError(theError);
  261.         return 2;
  262.         }
  263.     
  264. }
  265.